<?php 
class applyAction extends AdminMaster{
	public function execute($request){ 
		$action = $request->getParameter('action_name', '');
		$candidate_id = intval($request->getParameter('candidate_id', 0));
		$job_id = intval($request->getParameter('job_id', 0));
		$this->data['date_apply'] = DateHelper::dateFormat(date('Y-m-d'), time());
			
		//echo $candidate_id, $job_id, $action; die;
		
		if($action == ADMIN_DEFINED::SEARCH_CANDIDATE){
			$candidate = CandidatesTable::getDataById($candidate_id);
			$job = JobsTable::getDataById($job_id);
			$candidateToJob = CandidateToJobsTable::getDataByCandidateIdJobId($candidate_id, $job_id);
			
			if(count($candidate) && count($job)){
				$this->data['candidate']	= $candidate;
				$this->data['job']		= $job;
				$this->data['majors']		= MasterMajors::getArrayAllKeyId();
				
				
				$form = new CandidateToJobsFForm();
				if(count($candidateToJob)){
					$formData = array(
						'candidate_id'		=> $candidate_id,
						'job_id'			=> $job_id,
						'date_interview'	=> (empty($candidateToJob['date_interview'])?'': DateHelper::dateFormatFree($candidateToJob['date_interview'],'Y-m-d')),
						'probation_time'	=> $candidateToJob['probation_time'],
						'note_description'	=> $candidateToJob['note_description'],
						'comment_from_employer'	=> $candidateToJob['comment_from_employer'],
						'result_status'		=> $candidateToJob['result_status']
							
					);
					$form->bind($formData);
					$this->data['button_action'] = __('control_apply_update',null,'candidate_candidates');
					$this->data['action']	= ADMIN_DEFINED::APPLY_UPDATE;
					$this->data['date_apply'] = DateHelper::dateFormat($candidateToJob['date_apply']);
				}else{
					$formData = array(
							'candidate_id'		=> $candidate_id,
							'job_id'			=> $job_id,
							'date_interview'	=> date('d/m/Y',strtotime('+3 days')),
							'probation_time'	=> '0',
							'note_description'	=> '',
							'comment_from_employer'	=> '',
							'result_status'		=> '-1'
					);
					$form->bind($formData);
					$this->data['action']	= ADMIN_DEFINED::APPLY_INSERT;
					$this->data['button_action'] = __('control_apply',null,'candidate_candidates');
						
				}
				
				$this->form = $form;
				
			}else{
				$this->data['error'] = __('hacking_attemp',null,'table_list');
			}
		}elseif($action==ADMIN_DEFINED::APPLY_INSERT){
			$formData = array(
				'candidate_id'		=> $candidate_id,
				'job_id'			=> $job_id,
				'date_interview' 	=> $request->getParameter('date_interview', date('Y-m-d', strtotime('+3 days'))),
				'result_status' 	=> intval($request->getParameter('result_status', '-1')),
				'note_description' => $request->getParameter('note_description', ''),
				'comment_from_employer' => $request->getParameter('comment_from_employer', ''),
			);
			if(CandidateToJobsTable::insert($formData)){
				$data = array(
					'status'		=> '1',
					'data'			=> __('insert_success', null, 'candidate_candidates')
				);
			}else{
				$data = array(
					'status'		=> '0',
					'data'			=> __('insert_error', null, 'candidate_candidates')
				);
			}
			print_r(json_encode($data));
			die;
		}elseif($action==ADMIN_DEFINED::APPLY_UPDATE){
			$formData = array(
					'candidate_id'		=> $candidate_id,
					'job_id'			=> $job_id,
					'date_interview' 	=> $request->getParameter('date_interview', date('Y-m-d', strtotime('+3 days'))),
					'result_status' 	=> intval($request->getParameter('result_status', '-1')),
					'note_description' => $request->getParameter('note_description', ''),
					'comment_from_employer' => $request->getParameter('comment_from_employer', ''),
			);
			//print_r($formData); die;
			CandidateToJobsTable::updateByCandidateJob($formData);
			$data = array(
					'status'		=> '1',
					'data'			=> __('update_success', null, 'candidate_candidates')
			);
			/*}else{
				$data = array(
						'status'		=> '0',
						'data'			=> __('update_error', null, 'candidate_candidates')
				);
			}*/
			print_r(json_encode($data));
			die;
		}elseif($action == ADMIN_DEFINED::APPLY_DELETE){
			$id = $request->getParameter('id', '-1');
			if(CandidateToJobsTable::deleteById($id)){
				$data = array(
					'status'		=> '1',
					'data'			=> __('delete_success', null, 'candidate_candidates')
				);
			}else{
				$data = array(
						'status'		=> '0',
						'data'			=> __('delete_error', null, 'candidate_candidates')
				);
			}
			print_r(json_encode($data));
			die;
		}else{
			$this->data['error'] = __('hacking_attemp',null,'table_list');
		}
		$this->setVar('data', $this->data);
		$this->setVar('form', $this->form);
	}
}